<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/10/30
 * Time: 2:40
 */

namespace app\mocks\extend;


use app\common\exception\BaseException;
use app\common\model\Wxapp as WxappModel;
use think\Env;
use think\Exception;
use think\Request;

/**
 * 平台、代理商分账
 * Class MockWeChatProfitSharing
 * @package app\mocks\extend
 */
class MockWeChatProfitSharing
{
    protected static $instance;
    protected $request;
    protected $notifyParams;

    protected $config;
    protected $order;
    protected $dividendOrder;
    private $wxResponse = [];

    protected function __construct()
    {
        $this->request = Request::instance();
    }
    public static function instance()
    {
        if(!Env::get('mock.pay')) {
            return null;
        }
        if(self::$instance == null || (!self::$instance instanceof self)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function getResponse()
    {
        $this->wxResponse['return_code'] =  'SUCCESS';
        $this->wxResponse['result_code'] =  'SUCCESS';
        return $this->wxResponse;
    }

    public function addReceivers()
    {
        return true;
    }

    public function profitSharingApply($receivers = [], $profitSharingSn = [], $paidOrderTransactionId = [])
    {
        $this->wxResponse['profit_id'] = 'mock_profitsharing_apply_profit_id';
        return true;
    }

    public function dividendOver()
    {
        return true;
    }

    public function profitSharingQuery()
    {
        return true;
    }

    public function receiverAdd()
    {
        $ret = $this->profitsharingaddreceiver();
        if($ret['return_code'] != 'SUCCESS') {
            throw new Exception($ret['return_msg']);
        }
        if($ret['result_code'] != 'SUCCESS') {
            throw new Exception($ret['err_code_des']);
        }
        return true;
    }

    protected function profitsharingaddreceiver()
    {
        return [
            'return_code' => 'SUCCESS',
            'result_code' => 'SUCCESS'
        ];
    }

    public function dividendApply()
    {
        return true;
    }

    public function dividendApplyQuery()
    {
        return true;
    }

    public function setDividendOrder($order)
    {
        if(!$order->getData() || empty($order->getData())) {
            throw new Exception('setDividendOrder - 分账订单信息为空');
        }
        $this->dividendOrder = $order;
        return $this;
    }

    public function setUserOrder($order)
    {
        if(!$order->getData() || empty($order->getData())) {
            throw new Exception('setOrder - 订单信息为空');
        }
        $this->config = $this->wxConfig($order->wxapp_id);
        $this->order = $order;
        return $this;
    }


    public function wxConfig($wxappId)
    {

        $wxConfigs = WxappModel::getWxappCache($wxappId);
        //获取证书

        if (empty($wxConfigs['cert_pem']) || empty($wxConfigs['key_pem'])) {
            throw new BaseException(['msg' => '请先到后台小程序设置填写微信支付证书文件']);
        }
        $wxConfig['sub_mch_id'] = $wxConfigs['mchid'];
        $wxConfig['sub_app_id'] = $wxConfigs['app_id'];
        $wxConfig['md5_key']    = $wxConfigs['apikey'];
        // cert目录
        $filePath = __DIR__ . '/cert/' . $wxConfigs['wxapp_id'] . '/';
        $wxConfig['certPem']    = $filePath . 'cert.pem';
        $wxConfig['keyPem']     = $filePath . 'key.pem';
        return $wxConfig;
    }
}